package com.gdkf.dormitory.controller;


import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gdkf.dormitory.entity.Dorm;
import com.gdkf.dormitory.entity.MyPage;
import com.gdkf.dormitory.entity.Student;
import com.gdkf.dormitory.service.DormService;
import com.gdkf.dormitory.tools.MyPageTool;
import com.gdkf.dormitory.vo.Msg;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author lcy
 * @since 2022-09-09
 */
@RestController
public class DormController {
    @Autowired
    private DormService dormService;

    @PostMapping("/dorm/getAllDorm")
    public Msg getAllDorm(@RequestBody Object objectData){
        String s = objectData.toString().replaceAll("=",":");
        JSONObject templateObject = (JSONObject) JSONObject.parse(s);
//       获得第一次解析的数据
        MyPage myPage = MyPageTool.parseToPage(templateObject);

        Page<Dorm> page = new Page<>(myPage.getCurrentPage(),myPage.getSizePage());
        dormService.page(page, null);
        List<Dorm> records = page.getRecords();
        return  records.isEmpty()?Msg.fail():Msg.success()
                .add("dormList",page.getRecords())
                .add("sizePage",page.getSize())
                .add("currentPage",page.getCurrent())
                .add("pageTotal",page.getTotal());
    }

    @PostMapping("/dorm/getDormById")
    public Msg getOneDorm(@RequestBody Object objectData){
        String s = objectData.toString().replaceAll("=",":");
        JSONObject templateObject = (JSONObject) JSONObject.parse(s);
//       获得第一次解析的数据
        MyPage myPage = MyPageTool.parseToPage(templateObject);
        if(myPage.getObject()!=null){
            MyPage myPage2 = MyPageTool.JsonParseToDorm(myPage);
            Page<Dorm> page = new Page<>(myPage2.getCurrentPage(),myPage2.getSizePage());
            QueryWrapper<Dorm> wrapper = new QueryWrapper<>();
            wrapper.eq("dorm_id",((Dorm)myPage2.getObject()).getDormId());
            wrapper.eq("hous_id",((Dorm)myPage2.getObject()).getHousId());
            Page<Dorm> page1 = dormService.page(page, wrapper);
            List<Dorm> records = page1.getRecords();
            return records.isEmpty()?Msg.fail():Msg.success()
                    .add("dormList",records)
                    .add("sizePage",page.getSize())
                    .add("currentPage",page.getCurrent())
                    .add("pageTotal",page.getTotal());
        }
        return Msg.fail();
    }

    @PostMapping("/dorm/addDorm")
    public Msg addDorm(@RequestBody Dorm dorm){
        return dormService.save(dorm)?Msg.success():Msg.fail();
    }

    @PostMapping("/dorm/deleteDorm")
    public Msg deleteDorm(@RequestBody Dorm dorm){
        System.out.println(dorm);
        QueryWrapper<Dorm> wrapper = new QueryWrapper<>();
            wrapper.eq("dorm_id",dorm.getDormId());
            wrapper.eq("hous_id",dorm.getHousId());
            boolean remove = dormService.remove(wrapper);
            return remove?Msg.success():Msg.fail();
    }

    @PostMapping("/dorm/updateDorm")
    public Msg updateDorm(@RequestBody Dorm dorm){
        QueryWrapper<Dorm> wrapper = new QueryWrapper<>();
        wrapper.eq("dorm_id",dorm.getDormId());
        wrapper.eq("hous_id",dorm.getHousId());
        return dormService.update(dorm,wrapper)?Msg.success():Msg.fail();
    }
    @PostMapping("/dorm/getAllDromHId")
    public Msg getAllDromHId(@RequestBody Dorm dorm){
        System.out.println(dorm);
        QueryWrapper<Dorm> wrapper = new QueryWrapper<>();
        wrapper.select("DISTINCT hous_id");
        if(dorm.getDormId()!=""){
            wrapper.eq("dorm_Id",dorm.getDormId());
        }
        List<Dorm> list = dormService.list(wrapper);
        return list.isEmpty()?Msg.fail():Msg.success().add("HIdList",list);
    }
    @PostMapping("/dorm/getAllDromDId")
    public Msg getAllDromDId(@RequestBody Dorm dorm){
        QueryWrapper<Dorm> wrapper = new QueryWrapper<>();
        wrapper.select("DISTINCT dorm_id");
        if(dorm.getHousId()!=""){
            wrapper.eq("hous_Id",dorm.getHousId());
        }
        List<Dorm> list = dormService.list(wrapper);
        return list.isEmpty()?Msg.fail():Msg.success().add("DIdList",list);
    }

}

